<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet">
  <title>Document</title>
  <style>
    [v-cloak] {
      display: none;
    }

    .container {
      width: 800px;
      margin-top: 30px;
    }
  </style>
</head>

<body>
  <div id="app" class="container">
    <!-- 添加待办事项 -->
    <div id="todos" v-cloak>
      <div class="row">
        <div class="col-md-10">
          <input type="text" class="form-control" v-model.trim="todo">
        </div>
        <div class="col-md-2">
          <button class="btn btn-success" @click="addTodo">增加待办事项</button>
        </div>
      </div>
      <!-- 待办事项列表 -->
      <ul class="list-group" style="margin-top: 30px">
        <li class="list-group-item text-center" v-show="!todos.length">暂无待办事项</li>
        <li class="list-group-item" v-for="(item,index) in todos">
          {{item.title}}
          <div class="pull-right">
            <button class="btn btn-xs btn-primary" :disabled="item.status" @click="changeStatus(index)">{{item.status? '已完成':'未完成'}}</button>
            <button class="btn btn-xs btn-danger" @click="delTodo(index)">删除</button>
          </div>
        </li>
      </ul>
    </div>
  </div>
  <script src="../vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        todo: '',
        todos: [
          {
            title: '追番',
            status: true
          },
          {
            title: '成为唱见',
            status: false
          },
          {
            title: '成为舞见',
            status: false
          },
          {
            title: '成为up🐖',
            status: false
          }
        ]
      },
      methods: {
        //改变待办事项状态
        changeStatus(index){
          this.todos[index].status = true;
        },
        //添加待办事宜
        addTodo() {
          if(!this.todo.length) return;
          this.todos.push({
            title: this.todo,
            status: false
          });
          this.todo = ''
        },
        //删除待办事项
        delTodo(index){
          this.todos.splice(index,1);
        }
      }

    })
  </script>
</body>

</html>